package org.corf.payload;

import java.util.HashMap;
import java.util.Map;

public abstract class Payload {

    public static final String PAYLOAD_KEY_DELIMITER = ".";

    public static Payload create(PayloadType type, String id) {
        if (type == null) {
            throw new IllegalArgumentException("Payload type must not be null!");
        }
        if (id == null) {
            throw new IllegalArgumentException("Payload ID must not be null!");
        }
        Payload p = null;
        if (PayloadType.EVENT.equals(type)) {
            p = new Event(type, id);
        } else if (PayloadType.REQUEST.equals(type)) {
            p = new Request(type, id);
        } else if (PayloadType.RESPONSE.equals(type)) {
            p = new Response(type, id);
        } else {
            throw new IllegalArgumentException("Unsupported payload type '" + type.name() + "'!");
        }
        return p;
    }

    private final PayloadType type;
    private final String id;
    private final Map<String, String> header = new HashMap<String, String>();

    protected Payload(PayloadType type, String id) {
        this.type = type;
        this.id = id;
    }

    public Payload(String type, String id) {
        this.type = PayloadType.valueOf(type);
        this.id = id;
    }

    public PayloadType getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public String getHeader(String key) {
        return header.get(key);
    }

    public String setHeader(String key, String value) {
        return header.put(key, value);
    }

    public String toSignatureString() {
        String result = "";
        return result;
    }

    public String toString() {
        String result = toSignatureString();
        return result;
    }

}
